﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ZJGIS.Framework;
using ZJGIS.EngineControl;
using ZJGIS.Report.form;
using ZJGIS.Report.model;
using ZJGIS.ADF.BaseClasses;

namespace ZJGIS.Report.button
{
    internal class CustomStatButton:BaseDrawPolygonTool
    {
        private IFeatureLayer pFeatureLayer;
        IApplication m_Application = null;
        private StatLayer m_pStatLyr = null;
        IMapControl3 pMapControl = null; 

        public CustomStatButton()
        {
            base.m_caption = "选定区域统计";
            base.m_toolTip = base.m_caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
            base.OnCreate(hook);
        }

        public override bool Enabled
        {
            get
            {
                
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    IEnumLayer layers = (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer is IFeatureLayer)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                            return true;
                        }
                        layer = layers.Next();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                }
                return false;
            }
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "ZJGIS.Report.Res." + this.GetType().Name + ".png";
                System.IO.Stream s = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (s == null) return 0;
                return new Bitmap(s).GetHbitmap().ToInt32();
            }
        }

        public override void OnClick()
        {
            base.OnClick();

            if ((m_Application as IFramework).IsMapCtrlactive)
                pMapControl = (m_Application as IFramework).MapControl;
            else
                return;

            if (pMapControl == null || pMapControl.Map.LayerCount == 0) return;

            //pFeatureLayer = null;
            //string strContent = SystemConfig.systemConfigXml.统计.居民地;
            //string layerName = GetLayernameAndFields(strContent, out m_HashFields);
            //pFeatureLayer = CartoUtility.FindFeatureLayerByName((m_Application.Document as IMxDocument).ActiveView.FocusMap, layerName);
            //if (pFeatureLayer == null)
            //{
            //    ControlUtility.ShowInfomationMsgbox("尚未找到房屋面层，请加载房屋面层！");
            //    return;
            //}
        }

        public override void OnDblClick()
        {
            base.OnDblClick();
            if (m_geom == null || m_geom.IsEmpty)
            {
                ControlUtility.ShowInfomationMsgbox("自定范围为空！");
                return;
            }
            FrmCustomStat statForm = new FrmCustomStat();
            statForm.ShowDialog();
            if (statForm.DialogResult == DialogResult.OK )
            {
                m_pStatLyr = statForm.ReportStatLayer;
                string name = statForm.ReportName;
                if (m_pStatLyr == null)
                {
                    ControlUtility.ShowInfomationMsgbox("要素统计为空！");
                    return;
                }
                pFeatureLayer = CartoUtility.FindFeatureLayerByName(pMapControl.Map, m_pStatLyr.Value);
                if (pFeatureLayer == null)
                {
                    ControlUtility.ShowInfomationMsgbox("尚未找到" + m_pStatLyr.Value + "层，请先加载该层！");
                    return;
                }

                FeatureStat pStat = new FeatureStat();
                pStat.FeatureClass = pFeatureLayer.FeatureClass;
                pStat.MapControl = pMapControl;
                bool isSuccess = pStat.Stat(m_geom as IPolygon,m_pStatLyr, name);
            }

        }

    }
}
